package com.dyy.AI_System.controller;

import com.dyy.AI_System.domain.dto.LoginRequest;
import com.dyy.AI_System.domain.vo.ApiResponse;
import com.dyy.AI_System.domain.vo.LoginResponse;
import com.dyy.AI_System.service.IUsersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 认证相关控制器
 * </p>
 *
 * @author dyy
 */
@Tag(name = "认证管理", description = "登录、登出等认证相关API")
@RestController
@RequestMapping("/api")
public class AuthController {

    @Autowired
    private IUsersService usersService;

    /**
     * 用户登录
     */
    @Operation(summary = "用户登录", description = "用户登录接口，返回JWT token和用户信息")
    @PostMapping("/login")
    public ResponseEntity<ApiResponse<LoginResponse>> login(@Valid @RequestBody LoginRequest loginRequest) {
        try {
            // 调用登录服务
            LoginResponse loginResponse = usersService.login(loginRequest);
            return ResponseEntity.ok(ApiResponse.success(loginResponse));
        } catch (RuntimeException e) {
            // 处理登录失败的情况
            return ResponseEntity.ok(ApiResponse.fail(401, e.getMessage()));
        } catch (Exception e) {
            // 处理其他异常
            return ResponseEntity.ok(ApiResponse.fail(500, "登录失败，请稍后重试"));
        }
    }
}